package com.elinkway.infinitemovies.http.parser;

import com.elinkway.infinitemovies.bean.SubscriptionAlbum;
import com.elinkway.infinitemovies.bean.SubscriptionVideo;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhaopeipei on 2016/9/8.
 */
public class SubscriptionAlbumParser extends LVideoMobileParser<SubscriptionAlbum> {

    private static final String NAME = "name";

    private static final String DESC = "desc";

    private static final String SOURCE = "source";

    private static final String URL = "url";

    private static final String ICON = "icon";

    private static final String ALBUM_LIST = "album_list";

    private static final String AID = "aid";

    private static final String EPISODES = "episodes";

    private static final String NOW_EPISODE = "now_episode";

    private static final String VIDEO_LIST = "video_list";

    private static final String PORDER = "porder";

    private static final String DURATION = "duration";

    private static final String VIDEO_TYPE = "video_type";

    private static final String IMAGE = "image";

    private static final String SRC = "src";

    private static final String SUB_SRC = "sub_src";


    @Override
    public SubscriptionAlbum parse(JSONObject data) throws Exception {
        SubscriptionAlbum subscriptionAlbum = new SubscriptionAlbum();
        JSONObject obj = data;
        subscriptionAlbum.setName(obj.optString(NAME));
        subscriptionAlbum.setAid(obj.optString(AID));
        subscriptionAlbum.setEpisodes(Integer.parseInt(obj.optString(EPISODES)));
        subscriptionAlbum.setNowEpisode(obj.optString(NOW_EPISODE));
        subscriptionAlbum.setSub_src(obj.optString(SUB_SRC));
        subscriptionAlbum.setSrc(obj.optString(SRC));
        JSONArray array = obj.getJSONArray(VIDEO_LIST);
        List<SubscriptionVideo> subscriptionVideoList = new ArrayList<>();
        for (int j = 0; j < array.length(); j++) {
            SubscriptionVideo subscriptionVideo = new SubscriptionVideo();
            JSONObject object = array.getJSONObject(j);
            subscriptionVideo.setName(object.optString(NAME));
            subscriptionVideo.setUrl(object.optString(URL));
            subscriptionVideo.setPorder(object.optString(PORDER));
            subscriptionVideo.setEpisodes(object.optString(EPISODES));
            subscriptionVideo.setDuration(object.optString(DURATION));
            subscriptionVideo.setImage(object.optString(IMAGE));
            subscriptionVideo.setVideoType(object.optString(VIDEO_TYPE));
            subscriptionVideoList.add(subscriptionVideo);
        }
        subscriptionAlbum.setVideoList(subscriptionVideoList);
        return subscriptionAlbum;
    }

}
